$(function(){
	'use strict';
	 window.Validator = function(val,rule){		
		this.is_valid = function(new_val){
			var key;
			/*如果不是必填项且用户未填写任何内容则直接判定为合法*/
			if(new_val !== undefined) val = new_val;
			if(!rule.required && !val){
				return true;
			}
			/*防止重复检测*/
			for(key in rule){
				if(key === 'required')
				{
					continue;
				}
			/*调用rule中相对应的方法*/
			var r = this['validate_'+ key]();
			if(!r) return false;
			}
			return true;
		}
		this.validate_max = function(){
			pre_max_main();
			return val <= rule.max;
		}
		this.validate_min = function(){
			pre_max_main();			
			return val >= rule.min;
		}
		this.validate_maxlength = function(){
			return val.toString().length <= rule.maxlength;
		}
		this.validate_minlength = function(){
			return val.toString().length > rule.minlength;
		}
		this.validate_numeric = function(){
			return $.isNumeric(val);
		}
		this.validate_required = function(){
			var real = $.trim(val); 
			if(!real && real !==0) return false;
			return true;
				
		}
		this.validate_pattern = function (){
			var reg = new RegExp(rule.pattern);
			return reg.test(val);
		}
//		用于完成this.validate_mind的前置工作
		function pre_max_main(){
			val = parseFloat(val);
		}
	}
})
